<?php
!defined('P_W') && exit('Forbidden');
/**
 *
 * 二手房信息表db类
 * @author hu.liaoh
 *
 */
class PW_House_Sale_InfoDB extends BaseDB {

	var $_tableName = 'pw_house_saleinfo';
	var $_primaryKey = 'houseid';

	/**
	 *
	 * 根据houseid获取一条记录
	 * @param int $houseid
	 */
	function get($houseid) {
		$_sql = 'SELECT * FROM '. $this->_tableName .' WHERE houseid=' . pwEscape($houseid);
		$result = $this->_db->get_one($_sql);
		$indexTable = $result['state'] ? 'pw_house_saleoverdue' : 'pw_house_saleposttime_index';
		$indexinfo = $this->_db->get_one('SELECT * FROM ' . $indexTable . ' WHERE houseid=' . pwEscape($houseid));
		return is_array($indexinfo) ? array_merge($result, $indexinfo) : array();
	}
	
	/**
	 * 
	 * 获取多条房源信息
	 * @param $houseids
	 */
	function gets($houseids) {
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid IN (' . pwImplode($houseids) . ')';
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query, $this->_primaryKey);
		return $result;
	}
	
	/**
	 * 
	 * 获取相近房源
	 * @param $houseid
	 * @param $areatype
	 * @param $price
	 */
	function getSimilarList($houseid, $areatype, $price, $limit){
		$min = $price - $price * 0.1;
		$max = $price + $price * 0.1;
		$_sql = 'SELECT * FROM '. $this->_tableName .' WHERE areatype = ' . pwEscape($areatype) . ' AND houseid <> '. pwEscape($houseid) .' AND price >= ' . pwEscape($min) . ' AND price < ' . pwEscape($max) . $this->_Limit(0, $limit);
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query, $this->_primaryKey);
	}
	
	/**
	 * 
	 * 获取置顶信息
	 */
	function getTopHouseids(){
		$_sql = 'SELECT houseid, areatype, platetype, pricetype, acreagetype, roomtype, housetype FROM '. $this->_tableName .' WHERE istop = 1';
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query, $this->_primaryKey);
		return $result;
	}
	
	/**
	 * 
	 * 根据houseid从发布时间索引表中获取数据 
	 * @param $houseids
	 */
	function _getResultFromPosttime($houseids){
		$_sql = 'SELECT * FROM pw_house_saleposttime_index WHERE houseid IN( ' . pwImplode($houseids) . ')';
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query, $this->_primaryKey);
	}
	
	/**
	 * 
	 * 增加一条记录
	 * @param array $data 房源信息
	 */
	function add($data){
		$_sql = 'INSERT INTO ' . $this->_tableName .' SET '. S::sqlSingle($data);
		$this->_db->update($_sql);
		return $this->_db->insert_id();	
	}
	
	/**
	 * 
	 * 根据houseid修改一条记录
	 * @param $data
	 * @param $houseid
	 */
	function update($data, $houseid){
		return $this->_update($data, $houseid);
	}
	
	/**
	 * 
	 * 根据houseids获取uids
	 * @param $houseids
	 */
	function getUidsByHouseid($houseids){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'SELECT uid,houseid FROM ' . $this->_tableName . ' WHERE houseid IN(' .pwImplode($houseids). ')';
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query, $this->_primaryKey);
	}
	
	function getUserInfoByHouseid($houseids){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'SELECT uid,houseid,subject,username FROM ' . $this->_tableName . ' WHERE houseid IN(' .pwImplode($houseids). ')';
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query, $this->_primaryKey);
	}
	
	/**
	 *
	 *  根据houseid删除一条或多条记录
	 * @param int $houseid
	 */
	function delete($houseids){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'DELETE FROM ' . $this->_tableName . ' WHERE ' . $this->_primaryKey .' IN (' . pwImplode($houseids) . ')';
		return $this->_db->query($_sql);
	}
	
	function getBorkerSales($uid, $houseid, $limit){
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE uid = ' . pwEscape($uid) . 'AND houseid NOT IN(' .pwEscape($houseid). ') ORDER BY posttime ' . $this->_Limit(0, $limit);
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query, $this->_primaryKey);
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $houseids
	 */
	function getIndexInfo($houseids){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'SELECT unitprice.*, price.price, modifytime.modifytime, posttime.posttime FROM pw_house_saleunitprice_index unitprice, pw_house_saleprice_index price, pw_house_saleposttime_index posttime, pw_house_salemodifytime_index modifytime WHERE unitprice.houseid = price.houseid AND  price.houseid IN (' . pwImplode($houseids) . ')';
		$query = $this->_db->query($_sql);
		return $this->_getAllResultFromQuery($query, $this->_primaryKey);
	}
	
	/**
	 * 加急置顶房源信息
	 */
	function setHandler($houseids, $type, $value){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'UPDATE ' . $this->_tableName . ' SET is'. $type .'= '. pwEscape($value) .' WHERE houseid IN (' . pwImplode($houseids) . ')';
		return $this->_db->query($_sql);
	}
	
	
	/**
	 * 
	 * 设置房源状态
	 * @param $houseids
	 * @param $state
	 */
	function setState($houseids,$state){
		!is_array($houseids) && $houseids = array($houseids);
		if(empty($houseids)) return array();
		$_sql = 'UPDATE ' . $this->_tableName . ' SET state='. intval($state) .' WHERE houseid IN (' . pwImplode($houseids) . ')';
		return $this->_db->query($_sql);
	}
	
	/**
	 * 
	 * 根据type获取数据结构
	 */
	function getStruct($type){
		return array('areatype', 'platetype', 'pricetype', 'acreagetype', 'roomtype', 'housetype','subject', 'uid', 'username', 'isbroker', 'acreage', 'currentfloor', 'totalfloor', 'price', 'unitprice', 'room', 'righter', 'village', 'address', 'buildtime', 'traffic', 'fitment', 'equipment', 'thumb', 'descrip', 'gate', 'linkman', 'posttime', 'modifytime');
	}
	
}
?>